<template>
  <el-form ref="form" :model="SysUserApikey" label-width="80px">
    <el-form-item label="Api-Key">
      <el-input v-model="SysUserApikey.apiKey" id="user-apiKey" readonly clearable prefix-icon='el-icon-key' :style="{width: '80%'}"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">生成Key</el-button>
      <el-button type="success" size="mini" v-clipboard:copy="SysUserApikey.apiKey" v-clipboard:success="onCopy" v-clipboard:error="onCopyError" >复制到剪贴板</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="onCopyError"></right-toolbar>-->
    </el-form-item>
  </el-form>
</template>

<script>
import {updateApiKey, insertApiKey, getUserProfile} from "@/api/system/user";
export default {
  props: {
    SysUserApikey: {
      type: Object
    }
  },
  data() {
    return {
      //todo
    };
  },
  methods: {
    onCopy() {
      this.$message({
        message: this.SysUserApikey.apiKey,
        type: 'success'
      });
    },
    onCopyError() {
      this.$message({
        message: '警告，复制失败',
        type: 'warning'
      });
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.SysUserApikey.apiKey === ''){
            insertApiKey().then(response => {
            });
          }else {
            updateApiKey().then(response => {
            });
          }
          this.$modal.msgSuccess("成功");
          this.refresh();
        }
      });
    },
    refresh() {
      getUserProfile().then(response => {
        this.SysUserApikey.apiKey = response.SysUserApikey.apiKey
      })
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
